<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
	<head>
		<#include "../head.html">
		<title>角色管理</title>
		<script type="text/javascript">
		var queryForm,queryDialog,inputForm,inputDialog,grid,resourceGrid,userGrid;
		$(function(){
			queryForm = Rainbow.Form({
				  formId:"#queryForm"
				 ,lable:true
				 ,inputArray:[
					 {lable:"名称",name:"name",type:"textbox",condition:"like"}
				  ]
				 ,queryForm:true
			});
			
			queryDialog = Rainbow.Dialog({
		  		  title:"查询条件"
				 ,dialogId:'#queryDialog'
				 ,form:queryForm
				 ,width: '80%'
			 	 ,height: '50%'
		 		 ,modal: false
			     ,closable:true
			 	 ,buttons:[{
						text:'确认',
						iconCls:'icon-ok',
						handler:function(){
							grid.query();
							queryDialog.dialog('close');
						}
					}]
			});
			inputForm = Rainbow.Form({
				  formId:"#inputForm"
				 ,lable:true
				 ,inputArray:[
					 			 {name:"id",type:"hidden"}
					 			,{lable:"名称",name:"name",option:{required:true}}
					 			,{lable:"继承",name:"extend",type:"combobox",option:{multiple: true,multiline:true,data:getRoles()},style:{width:"400px"}}
	 						]
			 	 ,colNum:2
			});
			inputDialog = Rainbow.Dialog({
				  title:"角色"
				 ,width:'80%'
				 ,height:'80%'
				 ,dialogId:'#inputDialog'
				 ,form:inputForm
				 ,serviceName:"RoleService"
				 ,methodName:"save"
				 ,callback:function(result){
					 grid.query();
				 }
				 ,beforeSendData:function(data){
					 if(data.extend){
						var extend = "";
						for(var i=0;i<data.extend.length;i++){
							extend+=data.extend[i]+",";
						}
						data.extend=extend.substring(0,extend.lastIndexOf(","));
					 }
					 var authority ={name:data.name,authorityOperationEntitys:[]};
					 data.authority = authority;
					 if(data.id){
						 var authority_q = queryAuthority(data.id);
						 if(authority_q){
							 authority.id = authority_q.id;
						 }
					 }
					 
					 var nodes = resourceGrid.treegrid("getCheckedNodes");
					 for(var i=0;i<nodes.length;i++){
						 var authorityOperationEntity = {"operationEntityId":nodes[i].id};
						 authority.authorityOperationEntitys.push(authorityOperationEntity);
					 }
					 var users = userGrid.datagrid("getRows");
					 var userRoles = [];
					 if(users.length>0){
						 for(var i=0;i<users.length;i++){
							 userRoles.push({user:users[i]});
						 }
						 data["userRoles"] = userRoles;
					 }
			    	 
					 
				 }
			 });
			 
			grid = Rainbow.Grid({
				  serviceName:"RoleService"
				 ,queryForm:queryForm
				 ,columns: 
						[[
	  						 {field:'id',title:'选择',checkbox:true}
	  						,{field:'name',title:'名称',width:150}
	  						,{field:'extend',title:'继承',width:150
	  							,formatter: function(value,row,index){
	  								if(value){
	  									var roleNames="";
	  									var ids = value.split(",");
		  				        		for(var i=0;i<ids.length;i++){
		  				        			roleNames+=roleMap[ids[i]]+","
		  				        		}
		  				        		return roleNames.substring(0,roleNames.lastIndexOf(","));
	  								}
	  				        		
	  							}
	  						}
	 				    ]]
				,onDblClickRow: function (index, row) {
					grid.datagrid('selectRow',index);
				 	grid.update(inputDialog,inputForm);
		     	}
				,onUpdate:function(data){
					var nodes = resourceGrid.treegrid("getCheckedNodes");
					for(var i=0;i<nodes.length;i++){
						resourceGrid.treegrid("uncheckNode",nodes[i].id);
					}
					var authority_q = queryAuthority(data.id);
					if(authority_q && authority_q.authorityOperationEntitys){
						for(var i=0;i<authority_q.authorityOperationEntitys.length;i++){
							var authorityOperationEntity = authority_q.authorityOperationEntitys[i]
							resourceGrid.treegrid("checkNode",authorityOperationEntity.operationEntityId);
						}
					}
					var users = [];
					if(data.userRoles){
						for(var i=0;i<data.userRoles.length;i++){
							users.push(data.userRoles[i].user);
						}
					}
					userGrid.loadData(users);
				}
			});
			resourceGrid = new Rainbow.TreeGrid({
				 gridId:"#resource_grid"
				,treeField:"name"
				,serviceName:"ResourceService"
				,methodName:"query"
				,paths:[""]
				,width:"100%"
				,height:"auto"
				,toolbar:null
				,beforeLoad:function(filterOption){
					 filterOption.order=[{sort:1}]
				}
			 	,afterLoad:function(result){
			 		if(result.data){
			 			var data = result.data;
			 			for(i=0;i<data.length;i++){
			 				category = data[i];
			 				$.extend(category,JSON.parse(category.config));
			 				if(category.children){
			 					for(j=0;j<category.children.length;j++){
			 						menu = category.children[j];
			 						$.extend(menu,JSON.parse(menu.config));
			 						if(menu.children){
			 							for(k=0;k<menu.children.length;k++){
			 								button = menu.children[k];
			 								$.extend(button,JSON.parse(button.config));
			 							}
			 						}
			 					}
			 				}
			 			}
			 		}
			 	}
				,columns: [[
			         {field:'name',title:'名称',width:150}
			        ,{field:'type',title:'类型',width:100
			        	,formatter: function(value,row,index){
			        		return CodeCache.getNameFromList("resource",value);
						}
			        }
				]]
			});
			
			userGrid = Rainbow.Grid({
				  gridId:"#user_grid"
				 ,toolbar:"#user_grid_toolbar"
				 ,pagination:false
				 ,loadDate:false
				 ,rownumbers:true
				 ,deleteMode:"local"
				 ,width:"100%"
				 ,height:"auto"
				 ,columns: 
						[[
							 {field:'id',title:'选择',checkbox:true}
							,{field:'name',title:'姓名',width:150}
							,{field:'account.name',title:'账号',width:150
								,formatter: function(value,row,index){
									if(row.account){
										return row.account.name
									}
					        		
								}
							}
					    ]]
			});
			//UserQuery
			
			userQueryForm = Rainbow.Form({
				  formId:"#userQuery_queryForm"
				 ,lable:true
				 ,inputArray:[
					 {lable:"姓名",name:"name",type:"textbox",condition:"like"}
					,{lable:"账号",name:"account.name",type:"textbox",condition:"like",newline:true}
				  ]
				 ,queryForm:true
			});
			userQueryDialog = Rainbow.Dialog({
		  		  title:"用户查询"
				 ,dialogId:'#userQuery_dialog'
				 ,form:userQueryForm
				 ,width: 600
			 	 ,height: 400
		 		 ,modal: false
			     ,closable:true
			     ,buttons:[{
						text:'确认',
						iconCls:'icon-ok',
						handler:function(){
							var checkedRows = [].concat(userQueryGrid.datagrid("getChecked"));
							if(checkedRows.length>0){
								var rows = userGrid.datagrid("getRows");
								var rowsMap = {};
								for(var i=0;i<rows.length;i++){
									rowsMap[rows[i].id]=true;
								}
								for(var i=0;i<checkedRows.length;i++){
									if(!rowsMap[checkedRows[i].id]){
										userGrid.datagrid("appendRow",checkedRows[i]);
									}
									
								}
								userQueryDialog.dialog('close');
							}
						}
					}]
			});
			userQueryGrid = Rainbow.Grid({
				  gridId:"#userQuery_grid"
		  		 ,serviceName:"UserService"
	  			 ,toolbar:null
	  			 ,height: 265
				 ,queryForm:userQueryForm
				 ,paths:["account"]
				 ,columns: 
						[[
							 {field:'id',title:'选择',checkbox:true}
							,{field:'name',title:'姓名',width:150}
							,{field:'account.name',title:'账号',width:150
								,formatter: function(value,row,index){
									if(row.account){
										return row.account.name
									}
					        		
								}
							}
					    ]]
				,onDblClickRow: function (index, row) {
					grid.datagrid('selectRow',index);
				 	grid.update(inputDialog,inputForm);
		     	}
			});
		});
		var roles;
		var roleMap={};
		function getRoles(){
			if(!roles){
				var option = {
		                "serviceName":"RoleService"
		               ,"methodName":"query"
		               ,"params":[]
		               ,"async":false
		               ,"callback":function(result){
		            	   roles = result.data;
		            	   for(var i=0;i<roles.length;i++){
		            		   roleMap[roles[i].id] = roles[i].name;
		            	   }
		               }
		           };
				ServiceUnit.request(option);
			}
			return roles;
		}
		
		function queryAuthority(authorityEntityId){
			var authority;
			var filter = ServiceUnit.filter({"conditions":[{"propertyName":"authorityEntityId","operate":"eq",value:[authorityEntityId]}]});
			var option = {
                    "serviceName":"AuthorityService"
                   ,"methodName":"query"
                   ,"params":[filter]
                   ,"async":false
                   ,"callback":function(result){
                	   if(result.data && result.data.length==1){
                		   authority = result.data[0];
                	   }
                   }
               };
      		ServiceUnit.request(option);
      		return authority;
		}
		</script>
	</head>
	<body class="easyui-layout" data-options="fit:true">
		<div id="toolbar"></div>
		<div id="grid"></div>
		<div id="queryDialog">
			<div id="queryForm"></div>
		</div>
		<div id="inputDialog">
			<form id="inputForm"></form>
			<div id="tabs" class="easyui-tabs" style="width:100%;height:auto">
				<div title="资源配置">
					<div id="resource_grid"></div>
				</div>
				<div title="用户列表">
					<div id="user_grid_toolbar">
						<a href="#" class="easyui-linkbutton" iconCls="icon-add" onclick="userQueryDialog.dialog('open');" plain="true">添加</a>
		        		<a href="#" class="easyui-linkbutton" iconCls="icon-remove" onclick="userGrid.delete();" plain="true">删除</a>
					</div>
					<div id="user_grid"></div>
				</div>
			</div>
		</div>
		<div id="userQuery_dialog">
			<div id="userQuery_queryForm">
				<a href="#" class="easyui-linkbutton" iconCls="icon-search" style="margin:10px" onclick="userQueryGrid.query();">查询</a>
			</div>
			
			<div id="userQuery_grid"></div>
		</div>
	</body>
	<script type="text/javascript">
		Rainbow.Util.initButton("#toolbar");
	</script>
</html>